/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.grnt.buddy;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.grnt.dbcommon.DataControler.DataControlException;
import org.grnt.dbcommon.DataControler;
import static org.grnt.buddy.BuddiesContext.*;

/**
 * This is a servlet demonstrates how to move a buddy to a new parent.
 * @author sayid
 */
public class MoveBuddySlt extends HttpServlet {
   
	String objPath = null;
	String toPath  = null;
	Long objPathId = null;
	Long toPathId  =  null;
	String msg     = null;

	private final void output(HttpServletResponse response) throws IOException{
        response.setContentType("text/html;charset=UTF-8");
		response.setContentType("text/html");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		ServletOutputStream out = response.getOutputStream();
		out.println("<head><title>Just a basic servlet</title></head>");
		out.println("<body>");
		out.println("<br/>");
		out.println("object path is :" + objPath);
		out.println("<br/>");
		out.println("object path id is :" + objPathId);
		out.println("<br/>");
		out.println("<body>");
		out.println("<br/>");
		out.println("to path is :" + toPath);
		out.println("<br/>");
		out.println("to path id is :" + toPathId);
		out.println("<br/>");
		out.println("<h1>" + msg + "</h1>");
		out.println("</body>");
		out.close();
	}
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
		objPath = request.getParameter("objPath");
		toPath = request.getParameter("toPath");
		if((objPath == null) || ("".equals(objPath.trim()))) {
			msg = "The object path is empty.";
			output(response);
			return;
		}
		if((toPath == null)||("".equals(toPath.trim()))) {
			msg = "The to path is empty.";
			output(response);
			return;
		}
		if((objPathId = getBuddiesIndex().get(objPath)) == null){
			msg = "The object of moving path can't be found.";
			output(response);
			return;
		}
		if((toPathId = getBuddiesIndex().get(toPath)) == null) {
			msg = "The moving to path can't be found.";
			output(response);
			return;
		}
        try {
			MoveBuddy moveBuddy = new MoveBuddy(objPathId, toPathId);
			DataControler.update(moveBuddy);
			msg = "successful";
        } catch (DataControlException ex) {
			Logger.getLogger(MoveBuddySlt.class.getName()).log(Level.SEVERE, null, ex);
			msg = ex.getMessage();
			ex.printStackTrace();
		} finally {
            output(response);
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
